import time

from selenium.webdriver import Firefox
from selenium.webdriver.common.by import By

# 创建浏览器对象
driver = Firefox(executable_path=r'C:\Users\薛国鹏\.wdm\drivers\geckodriver\win64\v0.33.0\geckodriver.exe')

# 加载当当网
driver.get("https://www.dangdang.com/")
# 获取输入框
key = driver.find_element(By.ID,"key_S")
key.send_keys("科幻")
# 获取搜索框，点击搜索
search = driver.find_element(By.CSS_SELECTOR,".search .button")
search.click()
# 获取商品标题及价格
for i in range(5):
    shoplist = driver.find_elements(By.CSS_SELECTOR, ".shoplist li")
for li in shoplist:
    print(li.find_element(By.CSS_SELECTOR, "a").get_attribute("title"))
# 获取下一页按钮
next = driver.find_element(By.LINK_TEXT, "下一页")
next.click()